package boot.spring.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricProcessInstanceQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import boot.spring.pagemodel.DataGrid;
import boot.spring.pagemodel.HistoryProcess;
import boot.spring.pagemodel.MSG;
import boot.spring.pagemodel.RunningProcess;
import boot.spring.pagemodel.SecretTask;
import boot.spring.po.Role;
import boot.spring.po.SetSecretApply;
import boot.spring.po.User;
import boot.spring.po.User_role;
import boot.spring.service.SetSecretService;
import boot.spring.service.SystemService;
import io.swagger.annotations.Api;

@Api(value = "定密流程接口")
@Controller
public class SetSecretController {
	
	//@RequestMapping("setSecret1")
	
	@Autowired
	SetSecretService setSecretService;
	@Autowired
	SystemService systemservice;
	@Autowired
	TaskService taskservice;
	@Autowired
	RuntimeService runservice;
	@Autowired
	HistoryService histiryservice;
	
	@RequestMapping(value="/setsecret",method=RequestMethod.GET)
	String purchase(){
		return "setSecret/secretapply";
	}
	@RequestMapping(value="startsetsecret",method=RequestMethod.POST)
	@ResponseBody
	MSG startsetsecret(@RequestParam("remark")String remark,HttpSession session){
		String userid=(String) session.getAttribute("username");
		Map<String,Object> variables=new HashMap<String, Object>();
		variables.put("applyUserId", userid);
		SetSecretApply setSecretApply=new SetSecretApply();
		setSecretApply.setApplyer(userid);
		setSecretApply.setRemark(remark);
		setSecretApply.setApplytime(new Date());
		ProcessInstance ins=setSecretService.startWorkflow(setSecretApply, userid, variables);
		System.out.println("流程id"+ins.getId()+"已启动");
		return new MSG("sucess");
	}
	/**
	 * 跳转至部门领导审批页面
	 * @return
	 */
	@RequestMapping(value="/sdeptmleaderaudit",method=RequestMethod.GET)
	String spurchasemanager(){
		return "setSecret/deptmleaderaudit";
	}
	@RequestMapping(value="/secretmanagertasklist",method=RequestMethod.POST)
	@ResponseBody
	DataGrid<SecretTask> secretmanagertasklist(HttpSession session,@RequestParam("current") int current,@RequestParam("rowCount") int rowCount){
		DataGrid<SecretTask> grid=new DataGrid<SecretTask>();
		grid.setRowCount(rowCount);
		grid.setCurrent(current);
		grid.setTotal(0);
		grid.setRows(new ArrayList<SecretTask>());
		//先做权限检查，对于没有“部门经理”角色的用户,直接返回空
		String userid=(String) session.getAttribute("username");
		int uid=systemservice.getUidByusername(userid);
		User user=systemservice.getUserByid(uid);
		List<User_role> userroles=user.getUser_roles();
		if(userroles==null||userroles.size()==0)
			return grid;
		boolean flag=false;
		for(User_role ur:userroles)
		{
			Role r=ur.getRole();
			if(r.getRolename().equals("部门经理")){
				flag=true;
			}
		}
		if(flag){//有权限
			int firstrow=(current-1)*rowCount;
			List<SecretTask> results=new ArrayList<SecretTask>();
			List<Task> tasks=taskservice.createTaskQuery().taskCandidateGroup("部门经理").listPage(firstrow, rowCount);
			long totaltask=taskservice.createTaskQuery().taskCandidateGroup("部门经理").count();
			for(Task task:tasks){
				SecretTask vo=new SecretTask();
				String instanceid=task.getProcessInstanceId();
				ProcessInstance ins=runservice.createProcessInstanceQuery().processInstanceId(instanceid).singleResult();
				String businesskey=ins.getBusinessKey();
				SetSecretApply a=setSecretService.getSetSecretApply(Integer.parseInt(businesskey));
				if(a != null && !"".equals(a)) {
					vo.setApplyer(a.getApplyer());
					vo.setApplytime(a.getApplytime());
					vo.setBussinesskey(a.getId());
					vo.setRemark(a.getRemark());
					vo.setProcessdefid(task.getProcessDefinitionId());
					vo.setProcessinstanceid(task.getProcessInstanceId());
					vo.setTaskid(task.getId());
					vo.setTaskname(task.getName());
					results.add(vo);
				}
			}
			grid.setRowCount(rowCount);
			grid.setCurrent(current);
			grid.setTotal((int)totaltask);
			grid.setRows(results);
		}
		return grid;
	}
	
	/**
	 * 部门经理审核
	 * @param session
	 * @param taskid
	 * @param req
	 * @return
	 */
	@RequestMapping(value="task/secretmanagercomplete/{taskid}",method=RequestMethod.POST)
	@ResponseBody
	public MSG secretmanagercomplete(HttpSession session,@PathVariable("taskid") String taskid,HttpServletRequest req){
		String managerfalg=req.getParameter("managerfalg");
		String userid=(String) session.getAttribute("username");
		Map<String,Object> variables=new HashMap<String,Object>();
		variables.put("managerfalg", managerfalg);
		//设置会签需要的参数
		List<String> departList = new ArrayList<>();
		departList.add("A部门经理");
		departList.add("B部门经理");
		departList.add("C部门经理");
		variables.put("departList", departList);
		taskservice.claim(taskid, userid);
		taskservice.complete(taskid, variables);
		return new MSG("ok");
	}
	/**
	 * 我发起的定密流程
	 * @return
	 */
	@RequestMapping(value="/mysecret",method=RequestMethod.GET)
	String mypurchase(){
		return "setSecret/mysecret";
	}
	/**
	 * 我发起的定密流程数据
	 * @param session
	 * @param current
	 * @param rowCount
	 * @return
	 */
		@RequestMapping(value="mysecretprocess",method=RequestMethod.POST)
		@ResponseBody
		public DataGrid<RunningProcess> mysecretprocess(HttpSession session,@RequestParam("current") int current,@RequestParam("rowCount") int rowCount){
			int firstrow=(current-1)*rowCount;
			String userid=(String) session.getAttribute("username");
			ProcessInstanceQuery query = runservice.createProcessInstanceQuery();
			int total= (int) query.count();
			List<ProcessInstance> a = query.processDefinitionKey("givenSecret").involvedUser(userid).listPage(firstrow, rowCount);
			List<RunningProcess> list=new ArrayList<RunningProcess>();
			for(ProcessInstance p:a){
				RunningProcess process=new RunningProcess();
				if(p.getActivityId()==null)
				{//有子流程
					String father=p.getProcessInstanceId();
					String sonactiveid=runservice.createExecutionQuery().parentId(father).singleResult().getActivityId();//子流程的活动节点
					String sonexeid=runservice.createExecutionQuery().parentId(father).singleResult().getId();
					process.setActivityid(sonactiveid);
					//System.out.println(taskservice.createTaskQuery().executionId(sonexeid).singleResult().getName());
				}else{
					process.setActivityid(p.getActivityId());
				}
				process.setBusinesskey(p.getBusinessKey());
				process.setExecutionid(p.getId());
				process.setProcessInstanceid(p.getProcessInstanceId());
				SetSecretApply l=setSecretService.getSetSecretApply(Integer.parseInt(p.getBusinessKey()));
				if(l.getApplyer().equals(userid))
				list.add(process);
				else
				continue;
			}
			DataGrid<RunningProcess> grid=new DataGrid<RunningProcess>();
			grid.setCurrent(current);
			grid.setRowCount(rowCount);
			grid.setTotal(total);
			grid.setRows(list);
			return grid;
		}
		/**
		 * 跳转至A部门领导审批页面
		 * @return
		 */
		@RequestMapping(value="/Adeptmleaderaudit",method=RequestMethod.GET)
		String adeptmleaderaudit(){
			return "setSecret/adeptmleaderaudit";
		}
		/**
		 * A部门领导待审核列表数据
		 * @param session
		 * @param current
		 * @param rowCount
		 * @return
		 */
		@RequestMapping(value="/secretAmanagertasklist",method=RequestMethod.POST)
		@ResponseBody
		DataGrid<SecretTask> secretAmanagertasklist(HttpSession session,@RequestParam("current") int current,@RequestParam("rowCount") int rowCount){
			DataGrid<SecretTask> grid=new DataGrid<SecretTask>();
			grid.setRowCount(rowCount);
			grid.setCurrent(current);
			grid.setTotal(0);
			grid.setRows(new ArrayList<SecretTask>());
			//先做权限检查，对于没有A部门经理角色的用户,直接返回空
			String userid=(String) session.getAttribute("username");
			int uid=systemservice.getUidByusername(userid);
			User user=systemservice.getUserByid(uid);
			List<User_role> userroles=user.getUser_roles();
			if(userroles==null||userroles.size()==0)
				return grid;
			boolean flag=false;
			for(User_role ur:userroles)
			{
				Role r=ur.getRole();
				if(r.getRolename().equals("A部门经理")){
					flag=true;
				}
			}
			if(flag){//有权限
				int firstrow=(current-1)*rowCount;
				List<SecretTask> results=new ArrayList<SecretTask>();
				List<Task> tasks=taskservice.createTaskQuery().taskCandidateGroup("A部门经理").listPage(firstrow, rowCount);
				long totaltask=taskservice.createTaskQuery().taskCandidateGroup("A部门经理").count();
				for(Task task:tasks){
					SecretTask vo=new SecretTask();
					String instanceid=task.getProcessInstanceId();
					ProcessInstance ins=runservice.createProcessInstanceQuery().processInstanceId(instanceid).singleResult();
					String businesskey=ins.getBusinessKey();
					SetSecretApply a=setSecretService.getSetSecretApply(Integer.parseInt(businesskey));
					if(a != null && !"".equals(a)) {
						vo.setApplyer(a.getApplyer());
						vo.setApplytime(a.getApplytime());
						vo.setBussinesskey(a.getId());
						vo.setRemark(a.getRemark());
						vo.setProcessdefid(task.getProcessDefinitionId());
						vo.setProcessinstanceid(task.getProcessInstanceId());
						vo.setTaskid(task.getId());
						vo.setTaskname(task.getName());
						results.add(vo);
					}
				}
				grid.setRowCount(rowCount);
				grid.setCurrent(current);
				grid.setTotal((int)totaltask);
				grid.setRows(results);
			}
			return grid;
		}
		/**
		 * A部门经理审核
		 * @param session
		 * @param taskid
		 * @param req
		 * @return
		 */
		@RequestMapping(value="task/secretAmanagercomplete/{taskid}",method=RequestMethod.POST)
		@ResponseBody
		public MSG secretAmanagercomplete(HttpSession session,@PathVariable("taskid") String taskid,HttpServletRequest req){
			String pass=req.getParameter("pass");
			String userid=(String) session.getAttribute("username");
			Map<String,Object> variables=new HashMap<String,Object>();
			variables.put("pass", pass);
			taskservice.claim(taskid, userid);
			taskservice.complete(taskid, variables);
			return new MSG("ok");
		}
		/**
		 * 跳转至B部门领导审批页面
		 * @return
		 */
		@RequestMapping(value="/Bdeptmleaderaudit",method=RequestMethod.GET)
		String bdeptmleaderaudit(){
			return "setSecret/bdeptmleaderaudit";
		}
		/**
		 * B部门领导待审核列表数据
		 * @param session
		 * @param current
		 * @param rowCount
		 * @return
		 */
		@RequestMapping(value="/secretBmanagertasklist",method=RequestMethod.POST)
		@ResponseBody
		DataGrid<SecretTask> secretBmanagertasklist(HttpSession session,@RequestParam("current") int current,@RequestParam("rowCount") int rowCount){
			DataGrid<SecretTask> grid=new DataGrid<SecretTask>();
			grid.setRowCount(rowCount);
			grid.setCurrent(current);
			grid.setTotal(0);
			grid.setRows(new ArrayList<SecretTask>());
			//先做权限检查，对于没有B部门经理角色的用户,直接返回空
			String userid=(String) session.getAttribute("username");
			int uid=systemservice.getUidByusername(userid);
			User user=systemservice.getUserByid(uid);
			List<User_role> userroles=user.getUser_roles();
			if(userroles==null||userroles.size()==0)
				return grid;
			boolean flag=false;
			for(User_role ur:userroles)
			{
				Role r=ur.getRole();
				if(r.getRolename().equals("B部门经理")){
					flag=true;
				}
			}
			if(flag){//有权限
				int firstrow=(current-1)*rowCount;
				List<SecretTask> results=new ArrayList<SecretTask>();
				List<Task> tasks=taskservice.createTaskQuery().taskCandidateGroup("B部门经理").listPage(firstrow, rowCount);
				long totaltask=taskservice.createTaskQuery().taskCandidateGroup("B部门经理").count();
				for(Task task:tasks){
					SecretTask vo=new SecretTask();
					String instanceid=task.getProcessInstanceId();
					ProcessInstance ins=runservice.createProcessInstanceQuery().processInstanceId(instanceid).singleResult();
					String businesskey=ins.getBusinessKey();
					SetSecretApply a=setSecretService.getSetSecretApply(Integer.parseInt(businesskey));
					if(a != null && !"".equals(a)) {
						vo.setApplyer(a.getApplyer());
						vo.setApplytime(a.getApplytime());
						vo.setBussinesskey(a.getId());
						vo.setRemark(a.getRemark());
						vo.setProcessdefid(task.getProcessDefinitionId());
						vo.setProcessinstanceid(task.getProcessInstanceId());
						vo.setTaskid(task.getId());
						vo.setTaskname(task.getName());
						results.add(vo);
					}
				}
				grid.setRowCount(rowCount);
				grid.setCurrent(current);
				grid.setTotal((int)totaltask);
				grid.setRows(results);
			}
			return grid;
		}
		/**
		 * B部门经理审核
		 * @param session
		 * @param taskid
		 * @param req
		 * @return
		 */
		@RequestMapping(value="task/secretBmanagercomplete/{taskid}",method=RequestMethod.POST)
		@ResponseBody
		public MSG secretBmanagercomplete(HttpSession session,@PathVariable("taskid") String taskid,HttpServletRequest req){
			String pass=req.getParameter("pass");
			String userid=(String) session.getAttribute("username");
			Map<String,Object> variables=new HashMap<String,Object>();
			variables.put("pass", pass);
			taskservice.claim(taskid, userid);
			taskservice.complete(taskid, variables);
			return new MSG("ok");
		}
		/**
		 * 跳转至C部门领导审批页面
		 * @return
		 */
		@RequestMapping(value="/Cdeptmleaderaudit",method=RequestMethod.GET)
		String cdeptmleaderaudit(){
			return "setSecret/cdeptmleaderaudit";
		}
		/**
		 * C部门领导待审核列表数据
		 * @param session
		 * @param current
		 * @param rowCount
		 * @return
		 */
		@RequestMapping(value="/secretCmanagertasklist",method=RequestMethod.POST)
		@ResponseBody
		DataGrid<SecretTask> secretCmanagertasklist(HttpSession session,@RequestParam("current") int current,@RequestParam("rowCount") int rowCount){
			DataGrid<SecretTask> grid=new DataGrid<SecretTask>();
			grid.setRowCount(rowCount);
			grid.setCurrent(current);
			grid.setTotal(0);
			grid.setRows(new ArrayList<SecretTask>());
			//先做权限检查，对于没有A部门经理角色的用户,直接返回空
			String userid=(String) session.getAttribute("username");
			int uid=systemservice.getUidByusername(userid);
			User user=systemservice.getUserByid(uid);
			List<User_role> userroles=user.getUser_roles();
			if(userroles==null||userroles.size()==0)
				return grid;
			boolean flag=false;
			for(User_role ur:userroles)
			{
				Role r=ur.getRole();
				if(r.getRolename().equals("C部门经理")){
					flag=true;
				}
			}
			if(flag){//有权限
				int firstrow=(current-1)*rowCount;
				List<SecretTask> results=new ArrayList<SecretTask>();
				List<Task> tasks=taskservice.createTaskQuery().taskCandidateGroup("C部门经理").listPage(firstrow, rowCount);
				long totaltask=taskservice.createTaskQuery().taskCandidateGroup("C部门经理").count();
				for(Task task:tasks){
					SecretTask vo=new SecretTask();
					String instanceid=task.getProcessInstanceId();
					ProcessInstance ins=runservice.createProcessInstanceQuery().processInstanceId(instanceid).singleResult();
					String businesskey=ins.getBusinessKey();
					SetSecretApply a=setSecretService.getSetSecretApply(Integer.parseInt(businesskey));
					if(a != null && !"".equals(a)) {
						vo.setApplyer(a.getApplyer());
						vo.setApplytime(a.getApplytime());
						vo.setBussinesskey(a.getId());
						vo.setRemark(a.getRemark());
						vo.setProcessdefid(task.getProcessDefinitionId());
						vo.setProcessinstanceid(task.getProcessInstanceId());
						vo.setTaskid(task.getId());
						vo.setTaskname(task.getName());
						results.add(vo);
					}
				}
				grid.setRowCount(rowCount);
				grid.setCurrent(current);
				grid.setTotal((int)totaltask);
				grid.setRows(results);
			}
			return grid;
		}
		/**
		 * C部门经理审核
		 * @param session
		 * @param taskid
		 * @param req
		 * @return
		 */
		@RequestMapping(value="task/secretCmanagercomplete/{taskid}",method=RequestMethod.POST)
		@ResponseBody
		public MSG secretCmanagercomplete(HttpSession session,@PathVariable("taskid") String taskid,HttpServletRequest req){
			String pass=req.getParameter("pass");
			String userid=(String) session.getAttribute("username");
			Map<String,Object> variables=new HashMap<String,Object>();
			variables.put("pass", pass);
			taskservice.claim(taskid, userid);
			taskservice.complete(taskid, variables);
			return new MSG("ok");
		}
		/**
		 * 跳转至办公室经理审批页面
		 * @return
		 */
		@RequestMapping(value="/officeaudit",method=RequestMethod.GET)
		String officeaudit(){
			return "setSecret/officeaudit";
		}
		/**
		 * 办公室经理待审核列表数据
		 * @param session
		 * @param current
		 * @param rowCount
		 * @return
		 */
		@RequestMapping(value="/officemanagertasklist",method=RequestMethod.POST)
		@ResponseBody
		DataGrid<SecretTask> officemanagertasklist(HttpSession session,@RequestParam("current") int current,@RequestParam("rowCount") int rowCount){
			DataGrid<SecretTask> grid=new DataGrid<SecretTask>();
			grid.setRowCount(rowCount);
			grid.setCurrent(current);
			grid.setTotal(0);
			grid.setRows(new ArrayList<SecretTask>());
			//先做权限检查，对于没有“办公室经理”角色的用户,直接返回空
			String userid=(String) session.getAttribute("username");
			int uid=systemservice.getUidByusername(userid);
			User user=systemservice.getUserByid(uid);
			List<User_role> userroles=user.getUser_roles();
			if(userroles==null||userroles.size()==0)
				return grid;
			boolean flag=false;
			for(User_role ur:userroles)
			{
				Role r=ur.getRole();
				if(r.getRolename().equals("办公室经理")){
					flag=true;
				}
			}
			if(flag){//有权限
				int firstrow=(current-1)*rowCount;
				List<SecretTask> results=new ArrayList<SecretTask>();
				List<Task> tasks=taskservice.createTaskQuery().taskCandidateGroup("办公室经理").listPage(firstrow, rowCount);
				long totaltask=taskservice.createTaskQuery().taskCandidateGroup("办公室经理").count();
				for(Task task:tasks){
					SecretTask vo=new SecretTask();
					String instanceid=task.getProcessInstanceId();
					ProcessInstance ins=runservice.createProcessInstanceQuery().processInstanceId(instanceid).singleResult();
					String businesskey=ins.getBusinessKey();
					SetSecretApply a=setSecretService.getSetSecretApply(Integer.parseInt(businesskey));
					if(a != null && !"".equals(a)) {
						vo.setApplyer(a.getApplyer());
						vo.setApplytime(a.getApplytime());
						vo.setBussinesskey(a.getId());
						vo.setRemark(a.getRemark());
						vo.setProcessdefid(task.getProcessDefinitionId());
						vo.setProcessinstanceid(task.getProcessInstanceId());
						vo.setTaskid(task.getId());
						vo.setTaskname(task.getName());
						results.add(vo);
					}
				}
				grid.setRowCount(rowCount);
				grid.setCurrent(current);
				grid.setTotal((int)totaltask);
				grid.setRows(results);
			}
			return grid;
		}
		/**
		 * 办公室经理审核
		 * @param session
		 * @param taskid
		 * @param req
		 * @return
		 */
		@RequestMapping(value="task/officemanagercomplete/{taskid}",method=RequestMethod.POST)
		@ResponseBody
		public MSG officemanagercomplete(HttpSession session,@PathVariable("taskid") String taskid,HttpServletRequest req){
			String officeflag=req.getParameter("officeflag");
			String userid=(String) session.getAttribute("username");
			Map<String,Object> variables=new HashMap<String,Object>();
			variables.put("officeflag", officeflag);
			//设置是秘密级还是其他级
			//秘密—签发人审批，机密到所领导审批再到签发人审批
			String fileLevel = req.getParameter("fileLevel");
			//fileLevel = "secret";
			variables.put("fileLevel", fileLevel);
			taskservice.claim(taskid, userid);
			taskservice.complete(taskid, variables);
			return new MSG("ok");
		}
		/**
		 * 跳转至签发人审批页面
		 * @return
		 */
		@RequestMapping(value="/signeraudit",method=RequestMethod.GET)
		String signeraudit(){
			return "setSecret/signeraudit";
		}
		/**
		 * 签发人待审核列表数据
		 * @param session
		 * @param current
		 * @param rowCount
		 * @return
		 */
		@RequestMapping(value="/signertasklist",method=RequestMethod.POST)
		@ResponseBody
		DataGrid<SecretTask> signertasklist(HttpSession session,@RequestParam("current") int current,@RequestParam("rowCount") int rowCount){
			DataGrid<SecretTask> grid=new DataGrid<SecretTask>();
			grid.setRowCount(rowCount);
			grid.setCurrent(current);
			grid.setTotal(0);
			grid.setRows(new ArrayList<SecretTask>());
			//先做权限检查，对于没有“签发人”角色的用户,直接返回空
			String userid=(String) session.getAttribute("username");
			int uid=systemservice.getUidByusername(userid);
			User user=systemservice.getUserByid(uid);
			List<User_role> userroles=user.getUser_roles();
			if(userroles==null||userroles.size()==0)
				return grid;
			boolean flag=false;
			for(User_role ur:userroles)
			{
				Role r=ur.getRole();
				if(r.getRolename().equals("签发人")){
					flag=true;
				}
			}
			if(flag){//有权限
				int firstrow=(current-1)*rowCount;
				List<SecretTask> results=new ArrayList<SecretTask>();
				List<Task> tasks=taskservice.createTaskQuery().taskCandidateGroup("签发人").listPage(firstrow, rowCount);
				long totaltask=taskservice.createTaskQuery().taskCandidateGroup("签发人").count();
				for(Task task:tasks){
					SecretTask vo=new SecretTask();
					String instanceid=task.getProcessInstanceId();
					ProcessInstance ins=runservice.createProcessInstanceQuery().processInstanceId(instanceid).singleResult();
					String businesskey=ins.getBusinessKey();
					SetSecretApply a=setSecretService.getSetSecretApply(Integer.parseInt(businesskey));
					if(a != null && !"".equals(a)) {
						vo.setApplyer(a.getApplyer());
						vo.setApplytime(a.getApplytime());
						vo.setBussinesskey(a.getId());
						vo.setRemark(a.getRemark());
						vo.setProcessdefid(task.getProcessDefinitionId());
						vo.setProcessinstanceid(task.getProcessInstanceId());
						vo.setTaskid(task.getId());
						vo.setTaskname(task.getName());
						results.add(vo);
					}
				}
				grid.setRowCount(rowCount);
				grid.setCurrent(current);
				grid.setTotal((int)totaltask);
				grid.setRows(results);
			}
			return grid;
		}
		/**
		 * 签发人审核
		 * @param session
		 * @param taskid
		 * @param req
		 * @return
		 */
		@RequestMapping(value="task/signercomplete/{taskid}",method=RequestMethod.POST)
		@ResponseBody
		public MSG signercomplete(HttpSession session,@PathVariable("taskid") String taskid,HttpServletRequest req){
			String signerflag=req.getParameter("signerflag");
			String userid=(String) session.getAttribute("username");
			Map<String,Object> variables=new HashMap<String,Object>();
			variables.put("signerflag", signerflag);
			taskservice.claim(taskid, userid);
			taskservice.complete(taskid, variables);
			return new MSG("ok");
		}
		/**
		 * 跳转至档案室归档页面
		 * @return
		 */
		@RequestMapping(value="/archives",method=RequestMethod.GET)
		String archives(){
			return "setSecret/record";
		}
		/**
		 * 查询档案室归档人员待处理列表
		 * @param session
		 * @param current
		 * @param rowCount
		 * @return
		 */
		@RequestMapping(value="/recordtasklist",method=RequestMethod.POST)
		@ResponseBody
		DataGrid<SecretTask> recordtasklist(HttpSession session,@RequestParam("current") int current,@RequestParam("rowCount") int rowCount){
			DataGrid<SecretTask> grid=new DataGrid<SecretTask>();
			grid.setRowCount(rowCount);
			grid.setCurrent(current);
			grid.setTotal(0);
			grid.setRows(new ArrayList<SecretTask>());
			//先做权限检查，对于没有"档案室人员"角色的用户,直接返回空
			String userid=(String) session.getAttribute("username");
			int uid=systemservice.getUidByusername(userid);
			User user=systemservice.getUserByid(uid);
			List<User_role> userroles=user.getUser_roles();
			if(userroles==null||userroles.size()==0)
				return grid;
			boolean flag=false;
			for(User_role ur:userroles)
			{
				Role r=ur.getRole();
				if(r.getRolename().equals("档案室人员")){
					flag=true;
				}
			}
			if(flag){//有权限
				int firstrow=(current-1)*rowCount;
				List<SecretTask> results=new ArrayList<SecretTask>();
				List<Task> tasks=taskservice.createTaskQuery().taskCandidateGroup("档案室人员").listPage(firstrow, rowCount);
				long totaltask=taskservice.createTaskQuery().taskCandidateGroup("档案室人员").count();
				for(Task task:tasks){
					SecretTask vo=new SecretTask();
					String instanceid=task.getProcessInstanceId();
					ProcessInstance ins=runservice.createProcessInstanceQuery().processInstanceId(instanceid).singleResult();
					String businesskey=ins.getBusinessKey();
					SetSecretApply a=setSecretService.getSetSecretApply(Integer.parseInt(businesskey));
					if(a != null && !"".equals(a)) {
						vo.setApplyer(a.getApplyer());
						vo.setApplytime(a.getApplytime());
						vo.setBussinesskey(a.getId());
						vo.setRemark(a.getRemark());
						vo.setProcessdefid(task.getProcessDefinitionId());
						vo.setProcessinstanceid(task.getProcessInstanceId());
						vo.setTaskid(task.getId());
						vo.setTaskname(task.getName());
						results.add(vo);
					}
				}
				grid.setRowCount(rowCount);
				grid.setCurrent(current);
				grid.setTotal((int)totaltask);
				grid.setRows(results);
			}
			return grid;
		}
		/**
		 * 处理归档任务
		 * @param session
		 * @param taskid
		 * @param req
		 * @return
		 */
		@RequestMapping(value="task/recordcomplete/{taskid}",method=RequestMethod.POST)
		@ResponseBody
		public MSG recordcomplete(HttpSession session,@PathVariable("taskid") String taskid,HttpServletRequest req){
			String userid=(String) session.getAttribute("username");
			taskservice.claim(taskid, userid);
			taskservice.complete(taskid);
			return new MSG("ok");
		}
		/**
		 * 跳转至定密确认页面
		 * @return
		 */
		@RequestMapping(value="/secretcommit",method=RequestMethod.GET)
		String secretcommit(){
			return "setSecret/secretcommit";
		}
		/**
		 * 定密流程确认列表数据
		 * @param session
		 * @param current
		 * @param rowCount
		 * @return
		 */
		@RequestMapping(value="/secretcommittasklist",method=RequestMethod.POST)
		@ResponseBody
		DataGrid<SecretTask> secretcommittasklist(HttpSession session,@RequestParam("current") int current,@RequestParam("rowCount") int rowCount){
			int firstrow=(current-1)*rowCount;
			String userid=(String) session.getAttribute("username");
			TaskQuery query=taskservice.createTaskQuery().processDefinitionKey("givenSecret").taskCandidateOrAssigned(userid).taskDefinitionKey("submit");
			long total=query.count();
			List<Task> tasks=query.listPage(firstrow, rowCount);
			List<SecretTask> results=new ArrayList<SecretTask>();
				for(Task task:tasks){
					SecretTask vo=new SecretTask();
					String instanceid=task.getProcessInstanceId();
					ProcessInstance ins=runservice.createProcessInstanceQuery().processInstanceId(instanceid).singleResult();
					String businesskey=ins.getBusinessKey();
					SetSecretApply a=setSecretService.getSetSecretApply(Integer.parseInt(businesskey));
					if(a != null && !"".equals(a)) {
						vo.setApplyer(a.getApplyer());
						vo.setApplytime(a.getApplytime());
						vo.setBussinesskey(a.getId());
						vo.setRemark(a.getRemark());
						vo.setProcessdefid(task.getProcessDefinitionId());
						vo.setProcessinstanceid(task.getProcessInstanceId());
						vo.setTaskid(task.getId());
						vo.setTaskname(task.getName());
						results.add(vo);
					}
				}
				DataGrid<SecretTask> grid=new DataGrid<SecretTask>();
				grid.setRowCount(rowCount);
				grid.setCurrent(current);
				grid.setTotal((int)total);
				grid.setRows(results);
			return grid;
		}
		@RequestMapping(value="task/secretCommitComplete/{taskid}",method=RequestMethod.POST)
		@ResponseBody
		public MSG secretCommitComplete(HttpSession session,@PathVariable("taskid") String taskid,HttpServletRequest req){
			String userid=(String) session.getAttribute("username");
			taskservice.claim(taskid, userid);
			taskservice.complete(taskid);
			return new MSG("ok");
		}
		/**
		 * 进入定密流程历史记录
		 * @return
		 */
		@RequestMapping(value="/historySetSecretprocess",method=RequestMethod.GET)
		String historySetSecretprocess(){
			return "setSecret/historySetSecretprocess";
		}
		/**
		 * 定密流程历史记录数据
		 * @param session
		 * @param current
		 * @param rowCount
		 * @return
		 */
		@RequestMapping(value="getFinishSetSecretProcess",method=RequestMethod.POST)
		@ResponseBody
		public DataGrid<HistoryProcess> getFinishSetSecretProcess(HttpSession session,@RequestParam("current") int current,@RequestParam("rowCount") int rowCount){
			String userid=(String) session.getAttribute("username");
			HistoricProcessInstanceQuery process = histiryservice.createHistoricProcessInstanceQuery().processDefinitionKey("givenSecret").startedBy(userid).finished();
			int total= (int) process.count();
			int firstrow=(current-1)*rowCount;
			List<HistoricProcessInstance> info = process.listPage(firstrow, rowCount);
			List<HistoryProcess> list=new ArrayList<HistoryProcess>();
			for(HistoricProcessInstance history:info){
				HistoryProcess his=new HistoryProcess();
				String bussinesskey=history.getBusinessKey();
				SetSecretApply apply=setSecretService.getSetSecretApply(Integer.parseInt(bussinesskey));
				his.setSetSecretApply(apply);
				his.setBusinessKey(bussinesskey);
				his.setProcessDefinitionId(history.getProcessDefinitionId());
				list.add(his);
			}
			DataGrid<HistoryProcess> grid=new DataGrid<HistoryProcess>();
			grid.setCurrent(current);
			grid.setRowCount(rowCount);
			grid.setTotal(total);
			grid.setRows(list);
			return grid;
		}
		/**
		 * 定密流程历史记录详情
		 * @param ywh
		 * @return
		 */
		@RequestMapping(value = "secret/processhis", method = RequestMethod.POST)
		@ResponseBody
		public List<HistoricActivityInstance> processhis(@RequestParam("ywh") String ywh) {
			String instanceid = histiryservice.createHistoricProcessInstanceQuery().processDefinitionKey("givenSecret")
					.processInstanceBusinessKey(ywh).singleResult().getId();
			List<HistoricActivityInstance> his = histiryservice.createHistoricActivityInstanceQuery()
					.processInstanceId(instanceid).orderByHistoricActivityInstanceStartTime().asc().list();
			return his;
		}

}
